/**
 * @description 路由主配置文件
 */

import layout from '@/layout'
// createWebHashHistory
import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
  // {
  //   path: '/login',
  //   name: 'Login',
  //   meta: { title: '登录' },
  //   component: () => import('@views/login/index.vue')
  // }
]
const staticDemo = [
  {
    path: '/error-log',
    component: layout,
    redirect: '/error-log/list',
    meta: { title: 'ErrorLog', icon: 'bug' },
    children: [
      {
        path: 'list',
        component: () => import('@/views/error-log'),
        name: 'ErrorLog',
        meta: { title: 'Error Log' }
      },
      {
        path: 'error-log-test',
        component: () => import('@/views/error-log/ErrorLogTest.vue'),
        name: 'ErrorLogTest',
        meta: { title: 'ErrorLog Test' }
      }
    ]
  },
  {
    path: '/writing-demo',
    component: layout,
    meta: { title: 'Writing Demo', icon: 'eye-open' },
    alwaysShow: true,
    children: [
      {
        path: 'hook',
        component: () => import('@/views/example/hook/Hook.vue'),
        name: 'Hook',
        meta: { title: 'Hook-Demo' }
      },
      {
        path: 'vuex-use',
        component: () => import('@/views/example/vuex-use/VuexUse.vue'),
        name: 'VuexUse',
        meta: { title: 'Vuex-Demo' }
      },
      {
        path: 'mock-test',
        component: () => import('@/views/example/mock-test/MockTest.vue'),
        name: 'MockTest',
        meta: { title: 'Mock-Demo' }
      },
      {
        path: 'svg-icon',
        component: () => import('@/views/example/svg-icon/SvgIcon.vue'),
        name: 'SvgIcon',
        meta: { title: 'Svg-Demo' }
      },
      {
        path: 'work',
        component: () => import('@/views/example/worker/Worker.vue'),
        name: 'workIcon',
        meta: { title: 'WorkIcon' }
      },
      {
        path: 'parent-children',
        component: () => import('@/views/example/parent-children/Parent.vue'),
        name: 'Parent',
        meta: { title: 'Parent-Children' }
      },
      {
        path: 'keep-alive',
        component: () => import('@/views/example/keep-alive'),
        name: 'KeepAlive',
        //cachePage: cachePage when page enter, default false
        //leaveRmCachePage: remove cachePage when page leave, default false
        meta: { title: 'Keep-Alive', cachePage: true, leaveRmCachePage: false }
      },
      {
        path: 'tab-keep-alive',
        component: () => import('@/views/example/keep-alive/TabKeepAlive.vue'),
        name: 'TabKeepAlive',
        //closeTabRmCache: remove cachePage when tabs close, default false
        meta: { title: 'Tab-Keep-Alive', cachePage: true, closeTabRmCache: true }
      },
      {
        path: 'router-demo-f',
        name: 'routerDemoF',
        hidden: true,
        component: () => import('@/views/example/keep-alive/RouterDemoF.vue'),
        meta: { title: 'RouterDemo-F', activeMenu: '/writing-demo/keep-alive' }
      },
      {
        path: 'router-demo-s',
        name: 'routerDemoS',
        hidden: true,
        component: () => import('@/views/example/keep-alive/RouterDemoS.vue'),
        meta: { title: 'RouterDemo-S', activeMenu: '/writing-demo/keep-alive' }
      },
      {
        path: 'deep-router-keep-alive',
        name: 'DeepRouterKeepAlive',
        component: () => import('@/views/example/keep-alive/DeepRouterKeepAlive.vue'),
        //注：移除父容器页面缓存会把子页面一起移除了
        meta: { title: 'Deep KeepAlive', cachePage: true, leaveRmCachePage: true },
        alwaysShow: true,
        children: [
          {
            path: 'deep-children',
            name: 'DeepChildren',
            component: () => import('@/views/example/keep-alive/deep-children/DeepChildren.vue'),
            meta: { title: 'DeepChildren', cachePage: true, leaveRmCachePage: false }
          },
          {
            path: 'deep-children-sd',
            name: 'DeepChildrenSd',
            component: () => import('@/views/example/keep-alive/deep-children/DeepChildrenSd.vue'),
            meta: { title: 'DeepChildrenSd', cachePage: true, leaveRmCachePage: false }
          }
        ]
      }
    ]
  }
]
const constantRoutes = [
  {
    path: '/login',
    name: 'Login',
    meta: { title: '登录' },
    component: () => import('@views/login/index.vue'),
    hidden: true
  },
  {
    path: '/',
    redirect: '/dashboard',
    name: 'hiddendashboard',
    component: layout,
    meta: { title: '工作台', icon: 'dashboard', affix: true },
    hidden: true,
    children: [
      {
        path: '/dashboard',
        name: 'Dashboard',
        hidden: true,
        meta: { title: '', icon: 'dashboard', affix: true },
        component: () => import('@views/dashboard/index.vue')
      },

      {
        path: '/usercenter',
        name: 'Usercenter',
        hidden: true,
        meta: { title: '', icon: 'dashboard', affix: true },
        component: () => import('@views/dashboard/index.vue')
      }
    ]
  },
  {
    path: '/',
    // redirect: '/demos',
    name: 'Demos',
    component: layout,
    meta: { title: '组件示范', icon: 'dashboard', affix: true },
    hidden: false,
    children: [
      {
        path: 'examples',
        name: 'Demos',
        hidden: false,
        meta: { title: '组件', icon: 'dashboard', affix: true },
        component: () => import('@views/examples/index.vue')
      }
    ]
  },

  ...staticDemo

  // {
  //   path: '/:catchAll(.*)*',
  //   name: 'NotFound',
  //   // redirect: '/',
  //   component: () => import('@/views/404.vue'),
  //   hidden: true
  // }
]

//
// 获取@/views下的所有的.vue文件路径
const _allrouter = Object.keys(import.meta.glob('@/views/**/*.vue'))
const files = import.meta.glob('@/views/**/*.vue')
// console.log('_allrouter', _allrouter)
// console.log('files', files)
const handlePages = {}
const allPages = {}
let allrouter = []
// const arr = []
// _allrouter.forEach(item => {
//   arr.push(item)
// })
allrouter = _allrouter
// console.log(files)

function filesRouter(item) {
  let res = null

  for (const i in files) {
    if (i == item) {
      res = files[i].default
    }
  }
  return res
}
allrouter.forEach((item) => {
  const tmp = item.split('/')
  const fileName = tmp.pop()
  // console.log(fileName)
  const isComponent = allrouter.indexOf(tmp.join('/') + '/index.vue') !== -1
  if (fileName === 'index.vue' || !isComponent) {
    // console.log('--------------------------->'+ handlePages[item]['__file'].match(/cv-subsystem(\S*)/)[1])
    // const key = item.replace(/\.\//g, '/')
    //   .replace(/\.vue/g, '')
    //   .replace(/\/index/, '')

    const key = item
      .split('/views')[1]
      .replace(/\.vue/g, '')
      .replace(/\/index/, '')
    allPages[key] = files[item].default
  }
})

allPages['@/layout'] = layout
export { allPages, constantRoutes }

// 404
export const asyncRoutes = [
  {
    path: '/:pathMatch(.*)*',
    name: '404',
    component: () => import('@/views/404.vue'),
    hidden: true
  }
]

// console.log('allPages', allPages)

// console.log('constantRoutes', constantRoutes)
const router = createRouter({
  history: createWebHashHistory('/'),
  // history: createWebHashHistory(qiankunWindow.__POWERED_BY_QIANKUN__ ? '/micro/cloud-plat-mall-admin' : '/'),
  // linkActiveClass: 'active',
  scrollBehavior: () => ({ y: 0 }), // 使滚动条返回顶部
  routes: [...routes, ...constantRoutes]
})

// router.beforeEach((to, from, next) => {
//   NProgress.start()
//   document.title = getPageTitle(to.meta.title)
//   next()
// })

// router.afterEach((to) => {
//   NProgress.done()
//   document.title = getPageTitle(to.meta.title)
// })

export default router
